#pragma once
#include <QObject>
#include <QMap>
#include <QSharedPointer>
#include "DeviceNode.h"


class DeviceManager : public QObject {
    Q_OBJECT
public:
    static DeviceManager& instance() {
        static DeviceManager inst;
        return inst;
    }


    // 获取或创建节点
    QSharedPointer<DeviceNode> nodeFor(const QString& deviceName) {
        if (!m_nodes.contains(deviceName)) {
            auto node = QSharedPointer<DeviceNode>::create(deviceName);
            m_nodes.insert(deviceName, node);
            emit nodeAdded(deviceName);
        }
        return m_nodes[deviceName];
    }


    // 删除指定节点
    void removeNode(const QString& deviceName) {
        if (m_nodes.remove(deviceName)) {
            emit nodeRemoved(deviceName);
        }
    }


    // 清空所有节点并触发删除信号
    void clearAllNodes() {
        m_nodes.clear();
        emit allNodesCleared();
    }


signals:
    void nodeAdded(const QString& deviceName);
    void nodeRemoved(const QString& deviceName);
    void allNodesCleared();


private:
    DeviceManager() = default;
    QMap<QString, QSharedPointer<DeviceNode>> m_nodes;
};
